﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using myBoard.App.MagnetStore;
using myBoard.Framework;

namespace myBoard.App.Components.MagnetsStore.ViewsModels
{
    public class CategoriesViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<CategoryViewModel> _allCategories;
        private ObservableCollection<CategoryViewModel> _usedCategories;
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<CategoryViewModel> UsedCategories
        {
            get 
            {
                return _usedCategories; 
            }
        }

        public ObservableCollection<CategoryViewModel> AllCategories
        {
            get 
            {
                return _allCategories; 
            }
        }

        public void RefreshAllCategories()
        {
            MagnetsStoreClient.Client.GetAllCategoriesCompleted -= new EventHandler<myBoard.App.MagnetStore.GetAllCategoriesCompletedEventArgs>(Client_GetAllCategoriesCompleted); 
            MagnetsStoreClient.Client.GetAllCategoriesCompleted += new EventHandler<myBoard.App.MagnetStore.GetAllCategoriesCompletedEventArgs>(Client_GetAllCategoriesCompleted);
            _allCategories = new ObservableCollection<CategoryViewModel>();
            ObservableCollection<Category> categories = MagnetsStoreClient.Client.GetAllCategories();
            foreach (Category cat in categories)
                _allCategories.Add(new CategoryViewModel(cat));
            RaisePropertyChanged("AllCategories");
        }

        public void RefreshUsedCategories()
        {
            MagnetsStoreClient.Client.GetUsedCategoriesCompleted -= new EventHandler<myBoard.App.MagnetStore.GetUsedCategoriesCompletedEventArgs>(Client_GetUsedCategoriesCompleted);
            MagnetsStoreClient.Client.GetUsedCategoriesCompleted += new EventHandler<myBoard.App.MagnetStore.GetUsedCategoriesCompletedEventArgs>(Client_GetUsedCategoriesCompleted);
            ObservableCollection<Category> categories = MagnetsStoreClient.Client.GetUsedCategories();
            _usedCategories = new ObservableCollection<CategoryViewModel>();
            foreach (Category cat in categories)
                _usedCategories.Add(new CategoryViewModel(cat));
            RaisePropertyChanged("UsedCategories");
        }

        public void RefreshAllCategoriesAsync()
        {
            MagnetsStoreClient.Client.GetAllCategoriesCompleted -= new EventHandler<myBoard.App.MagnetStore.GetAllCategoriesCompletedEventArgs>(Client_GetAllCategoriesCompleted);
            MagnetsStoreClient.Client.GetAllCategoriesCompleted += new EventHandler<myBoard.App.MagnetStore.GetAllCategoriesCompletedEventArgs>(Client_GetAllCategoriesCompleted);
            MagnetsStoreClient.Client.GetAllCategoriesAsync();
        }

        public void RefreshUsedCategoriesAsync()
        {
            MagnetsStoreClient.Client.GetUsedCategoriesCompleted -= new EventHandler<myBoard.App.MagnetStore.GetUsedCategoriesCompletedEventArgs>(Client_GetUsedCategoriesCompleted);
            MagnetsStoreClient.Client.GetUsedCategoriesCompleted += new EventHandler<myBoard.App.MagnetStore.GetUsedCategoriesCompletedEventArgs>(Client_GetUsedCategoriesCompleted);
            MagnetsStoreClient.Client.GetUsedCategoriesAsync();
        }

        void Client_GetUsedCategoriesCompleted(object sender, myBoard.App.MagnetStore.GetUsedCategoriesCompletedEventArgs e)
        {
            _usedCategories = new ObservableCollection<CategoryViewModel>();
            if (e.Error != null)
                ExceptionsManager.LogException("Magnets Store", e.Error, true);
            ObservableCollection<Category> categories = e.Result;
            foreach (Category cat in categories)
                _usedCategories.Add(new CategoryViewModel(cat));
            RaisePropertyChanged("UsedCategories");
        }

        void Client_GetAllCategoriesCompleted(object sender, myBoard.App.MagnetStore.GetAllCategoriesCompletedEventArgs e)
        {
            _allCategories = new ObservableCollection<CategoryViewModel>();
            if (e.Error != null)
                ExceptionsManager.LogException("Magnets Store", e.Error, true);
            ObservableCollection<Category> categories = e.Result;
            foreach (Category cat in categories)
                _allCategories.Add(new CategoryViewModel(cat));
            RaisePropertyChanged("AllCategories");
        }               

        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
